package spotGUI.personalProfileGUI;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import personalProfile.FamilyManager;
import personalProfile.FamilyManager.FamilyComponent;
import spotGUI.utils.FamilyListCellRenderer;
import java.util.ResourceBundle;

public class FamilyPanel extends JPanel {
	
	private JScrollPane scrollPane;
	private JPanel panel;
	
	private FamilyManager manager;
	
	private JLabel lblParents;
	private JLabel lblGrandParents;
	private JLabel lblCousins;
	private JLabel lblFriends;
	private JLabel lblUncles;
	private JLabel lblBoyfriends;
	
	public static final int FIXED_CELL_WIDTH = 128;
	
	private JList listParents;
	private JList listGrandParents;
	private JList listCousins;
	private JList listFriends;
	private JList listUncles;
	private JList listBoyfriends;
	
	private final Font font = new Font("Agency FB", Font.BOLD, 23);
	
	private AddFamilyComponentPanel addCompPanel;


	public FamilyPanel(FamilyManager manager) {
		setBorder(new EmptyBorder(10, 10, 10, 10));
		
		this.manager = manager;
		
		setLayout(new BorderLayout(0, 0));
		
		panel = new JPanel();
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		scrollPane = new JScrollPane(panel);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setOpaque(false);

		add(scrollPane, BorderLayout.CENTER);
		
		setOpaque(false);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{396, 0};
		gridBagLayout.rowHeights = new int[]{16, 7, 11, 9, 12, 8, 13, 7, 11, 8, 13, 3, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gridBagLayout);
		
		addCompPanel = new AddFamilyComponentPanel(manager, this);
		addCompPanel.setVisible(false);
		add(addCompPanel, BorderLayout.NORTH);
		
		lblBoyfriends = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblBoyfriends.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblBoyfriends.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/red_heart.png")));
		lblBoyfriends.setFont(font);
		GridBagConstraints gbc_lblBoyfriends = new GridBagConstraints();
		gbc_lblBoyfriends.anchor = GridBagConstraints.WEST;
		gbc_lblBoyfriends.insets = new Insets(0, 0, 5, 0);
		gbc_lblBoyfriends.gridx = 0;
		gbc_lblBoyfriends.gridy = 0;
		panel.add(lblBoyfriends, gbc_lblBoyfriends);
		
		listBoyfriends = new JList();
		listBoyfriends.setCellRenderer(new FamilyListCellRenderer());
		listBoyfriends.setModel(new DefaultListModel());
		listBoyfriends.setOpaque(false);
		listBoyfriends.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listBoyfriends.setFixedCellWidth(FIXED_CELL_WIDTH);
		listBoyfriends.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_listBoyfriends = new GridBagConstraints();
		gbc_listBoyfriends.insets = new Insets(0, 0, 5, 0);
		gbc_listBoyfriends.fill = GridBagConstraints.BOTH;
		gbc_listBoyfriends.gridx = 0;
		gbc_listBoyfriends.gridy = 1;
		panel.add(listBoyfriends, gbc_listBoyfriends);

		lblFriends = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblFriends.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblFriends.setFont(font);
		lblFriends.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/favorite.png")));
		GridBagConstraints gbc_lblFriends = new GridBagConstraints();
		gbc_lblFriends.anchor = GridBagConstraints.WEST;
		gbc_lblFriends.insets = new Insets(0, 0, 5, 0);
		gbc_lblFriends.gridx = 0;
		gbc_lblFriends.gridy = 2;
		panel.add(lblFriends, gbc_lblFriends);

		listFriends = new JList();
		listFriends.setCellRenderer(new FamilyListCellRenderer());
		listFriends.setModel(new DefaultListModel());
		listFriends.setOpaque(false);
		listFriends.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listFriends.setFixedCellWidth(FIXED_CELL_WIDTH);
		listFriends.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_friends = new GridBagConstraints();
		gbc_friends.fill = GridBagConstraints.BOTH;
		gbc_friends.insets = new Insets(0, 0, 5, 0);
		gbc_friends.gridx = 0;
		gbc_friends.gridy = 3;
		panel.add(listFriends, gbc_friends);

		lblParents = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblParents.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblParents.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/home.png")));
		lblParents.setFont(font);
		GridBagConstraints gbc_lblParents = new GridBagConstraints();
		gbc_lblParents.anchor = GridBagConstraints.WEST;
		gbc_lblParents.insets = new Insets(0, 0, 5, 0);
		gbc_lblParents.gridx = 0;
		gbc_lblParents.gridy = 4;
		panel.add(lblParents, gbc_lblParents);



		listParents = new JList();
		listParents.setCellRenderer(new FamilyListCellRenderer());
		listParents.setModel(new DefaultListModel());
		listParents.setOpaque(false);
		listParents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listParents.setFixedCellWidth(FIXED_CELL_WIDTH);
		listParents.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_Parents = new GridBagConstraints();
		gbc_Parents.fill = GridBagConstraints.BOTH;
		gbc_Parents.insets = new Insets(0, 0, 5, 0);
		gbc_Parents.gridx = 0;
		gbc_Parents.gridy = 5;
		panel.add(listParents, gbc_Parents);



		lblGrandParents = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblGrandParents.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblGrandParents.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/home.png")));
		lblGrandParents.setFont(font);
		GridBagConstraints gbc_lblGrandParents = new GridBagConstraints();
		gbc_lblGrandParents.anchor = GridBagConstraints.WEST;
		gbc_lblGrandParents.insets = new Insets(0, 0, 5, 0);
		gbc_lblGrandParents.gridx = 0;
		gbc_lblGrandParents.gridy = 6;
		panel.add(lblGrandParents, gbc_lblGrandParents);


		listGrandParents = new JList();
		listGrandParents.setCellRenderer(new FamilyListCellRenderer());
		listGrandParents.setModel(new DefaultListModel());
		listGrandParents.setOpaque(false);
		listGrandParents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listGrandParents.setFixedCellWidth(FIXED_CELL_WIDTH);
		listGrandParents.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_GrandParents = new GridBagConstraints();
		gbc_GrandParents.fill = GridBagConstraints.BOTH;
		gbc_GrandParents.insets = new Insets(0, 0, 5, 0);
		gbc_GrandParents.gridx = 0;
		gbc_GrandParents.gridy = 7;
		panel.add(listGrandParents, gbc_GrandParents);

		lblCousins = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblCousins.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblCousins.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/home.png")));
		lblCousins.setFont(font);
		GridBagConstraints gbc_lblCousins = new GridBagConstraints();
		gbc_lblCousins.anchor = GridBagConstraints.WEST;
		gbc_lblCousins.insets = new Insets(0, 0, 5, 0);
		gbc_lblCousins.gridx = 0;
		gbc_lblCousins.gridy = 8;
		panel.add(lblCousins, gbc_lblCousins);



		listCousins = new JList();
		listCousins.setCellRenderer(new FamilyListCellRenderer());
		listCousins.setModel(new DefaultListModel());
		listCousins.setOpaque(false);
		listCousins.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listCousins.setFixedCellWidth(FIXED_CELL_WIDTH);
		listCousins.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_Counsins = new GridBagConstraints();
		gbc_Counsins.fill = GridBagConstraints.BOTH;
		gbc_Counsins.insets = new Insets(0, 0, 5, 0);
		gbc_Counsins.gridx = 0;
		gbc_Counsins.gridy = 9;
		panel.add(listCousins, gbc_Counsins);

		lblUncles = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("FamilyPanel.lblUncles.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblUncles.setIcon(new ImageIcon(FamilyPanel.class.getResource("/images/Icons/24x24/home.png")));
		lblUncles.setFont(font);
		GridBagConstraints gbc_lblUncles = new GridBagConstraints();
		gbc_lblUncles.anchor = GridBagConstraints.WEST;
		gbc_lblUncles.insets = new Insets(0, 0, 5, 0);
		gbc_lblUncles.gridx = 0;
		gbc_lblUncles.gridy = 10;
		panel.add(lblUncles, gbc_lblUncles);


		listUncles = new JList();
		listUncles.setCellRenderer(new FamilyListCellRenderer());
		listUncles.setModel(new DefaultListModel());
		listUncles.setOpaque(false);
		listUncles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listUncles.setFixedCellWidth(FIXED_CELL_WIDTH);
		listUncles.setLayoutOrientation(JList.VERTICAL_WRAP);
		GridBagConstraints gbc_Uncles = new GridBagConstraints();
		gbc_Uncles.fill = GridBagConstraints.BOTH;
		gbc_Uncles.gridx = 0;
		gbc_Uncles.gridy = 11;
		panel.add(listUncles, gbc_Uncles);


		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
					updateAllListLayouts();
			}
		});

	}

	public void fillList (FamilyComponent component) {
		switch (component.type) {
			case FamilyManager.BOYFRIEND:
				((DefaultListModel)listBoyfriends.getModel()).addElement(component);
				break;
			case FamilyManager.COUSIN:
				((DefaultListModel)listCousins.getModel()).addElement(component);
				break;
			case FamilyManager.FRIEND:
				((DefaultListModel)listFriends.getModel()).addElement(component);
				break;
			case FamilyManager.GRANDPARENT:
				((DefaultListModel)listGrandParents.getModel()).addElement(component);
				break;
			case FamilyManager.PARENT:
				((DefaultListModel)listParents.getModel()).addElement(component);
				break;
			case FamilyManager.UNCLE:
				((DefaultListModel)listUncles.getModel()).addElement(component);
				break;
			}
	}

	private void updateListLayout (JList list, JScrollPane scrollPane) {
		int columnCount = scrollPane.getWidth() / FIXED_CELL_WIDTH;
		int rowCount = list.getModel().getSize()/columnCount;
		if (list.getModel().getSize()%columnCount != 0) {
			rowCount++;
		}
		list.setVisibleRowCount(rowCount);
	}
	
	public void updateAllListLayouts() {
		updateListLayout(listBoyfriends, scrollPane);
		updateListLayout(listParents, scrollPane);
		updateListLayout(listCousins, scrollPane);
		updateListLayout(listFriends, scrollPane);
		updateListLayout(listGrandParents, scrollPane);
		updateListLayout(listUncles, scrollPane);
	}
	
	public void makeEditable () {
		addCompPanel.setVisible(true);
		ListSelectionListener listener = new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
					JList list = ((JList)e.getSource());
					FamilyComponent component = (FamilyComponent) list.getSelectedValue();
					if (component != null) {
						((DefaultListModel)list.getModel()).removeElement(component);
						manager.addPendingRemoveComponent(component);
					}
				}
			}
		};
		listBoyfriends.addListSelectionListener(listener);
		listParents.addListSelectionListener(listener);
		listCousins.addListSelectionListener(listener);
		listFriends.addListSelectionListener(listener);
		listGrandParents.addListSelectionListener(listener);
		listUncles.addListSelectionListener(listener);
	}

}
